/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.common.school.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.school.entity.School;
import cn.apestar.modules.common.school.service.SchoolService;

/**
 * 学校Controller
 * @author LVH
 * @version 2016-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/school/school")
public class SchoolController extends BaseController {

    @Autowired
    private SchoolService schoolService;

    @ModelAttribute
    public School get(@RequestParam(required = false) String id) {
        School entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = schoolService.get(id);
        }
        if (entity == null) {
            entity = new School();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(School school, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<School> page = schoolService.findPage(new Page<School>(request, response), school);
        model.addAttribute("page", page);
        return "modules/school/schoolList";
    }

    @RequestMapping(value = "form")
    public String form(School school, Model model) {
        model.addAttribute("school", school);
        return "modules/school/schoolForm";
    }

    @RequestMapping(value = "save")
    public String save(School school, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, school)) {
            return form(school, model);
        }
        schoolService.save(school);
        addMessage(redirectAttributes, "保存学校成功");
        return "redirect:" + Global.getAdminPath() + "/school/school/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(School school, RedirectAttributes redirectAttributes) {
        schoolService.delete(school);
        addMessage(redirectAttributes, "删除学校成功");
        return "redirect:" + Global.getAdminPath() + "/school/school/?repage";
    }

}